<?php
/* 
 * 幼儿园园地
 *  */
namespace Admin\Controller;

class CornerController extends ComController{
	//后台列表页
	public  function  index(){
		$title = $_GET['title']?trim($_GET['title']):'';
		if(!empty($title)){
			$where['title']=array('like','%'.$title.'%');
		}
		$sc_id = $this->userType();
		$corner = M('Corner');
		$where['sc_id']=$sc_id;
		$where['status']=1;
		
		$p= intval($_GET['p'])?$_GET['p']:1;
		$pagesize = 15;#每页数量
		$offset = $pagesize * ($p - 1);//计算记录偏移量
		
		$count = $corner->where($where)->count();
		$data = $corner->where($where)->limit($offset . ',' . $pagesize)->select();

		if(!empty($data)){
			$url='';
			foreach ($data as $key=>$val){
				$url=$val['content'];
				$data[$key]['content']=explode('|', $url);
			}
			$page = new \Think\Page($count, $pagesize);
			$page = $page->show();
			$this->assign('page',$page);
			$this->assign('data',$data);
		}else{
			//没有发布园地
		}
		$this->display();
	
	}
	//后台添加页
	public function  add(){
		
		if(IS_POST){
			$data['sc_id'] = $this->userType();
			$data['title']= isset($_POST['title']) ? trim($_POST['title']) : '';
			$data['content']= isset($_POST['content']) ? trim($_POST['content']) : '';
			if(empty($data['title'])){
				$this->error('请填写幼儿园地标题！');
			}
			if(empty($data['content'])){
				$this->error('请添加幼儿园地图片!');
			}
			$data['add_time']=time();
			$corner = M('Corner');
			$res = $corner->add($data);
			if($res){
				$this->success('添加成功');
			}else {
				$this->error('添加失败');
			}
		}else{
			$this->display();
		}
		
	}
	//园地修改页面
	public function  edit(){
		if(IS_POST){
			$data['id']=isset($_POST['id']) ? trim($_POST['id']) : $_GET['id'];
			$data['title']= isset($_POST['title']) ? trim($_POST['title']) : '';
			$data['content']= isset($_POST['content']) ? trim($_POST['content']) : '';
			if(empty($data['title'])){
				$this->error('请填写幼儿园地标题！');
			}
			if(empty($data['content'])){
				$this->error('请添加幼儿园地图片!');
			}
			if(empty($data['id'])){
				$this->error('参数错误!');
			}
			$data['update_time']=time();
			$corner = M('Corner');
			$res = $corner->save($data);
			if($res){
				$this->success('编辑成功',U('Corner/index'));
			}else {
				$this->error('编辑失败');
			}
		}else{
			$id= isset($_GET['id']) ? trim($_GET['id']) : '';
			if(empty($id)){
				$this->error('参数错误');
			}else{
				$where['id']=$id;
			}
			$corner= M('corner');
			$data = $corner->where($where)->find();
			if($data){
				$this->assign('data',$data);
				$this->display();
			}else{
				$this->error('参数错误');
			}
		}
		

	}
	//园地删除页面
	public function del(){
		$id= isset($_GET['id']) ? trim($_GET['id']) : '';
		if(empty($id)){
			$this->error('参数错误，重新选择');
		}
		$corner = M('Corner');
		$data['id']=$id;
// 		删除数据库成功后，将本地图片删除，避免造成垃圾图片
		$prcUrl = $corner->field('content')->where($data)->find();
		$res = $corner->where($data)->delete();
		if($res){
			$prcUrl=explode('|', $prcUrl['content']);
			foreach ($prcUrl as $v){
				@unlink('.'.$v);
			}
			$this->success('删除成功',$_SERVER['HTTP_REFERER']);
		}else{
			$this->error('删除失败，请重新操作',$_SERVER['HTTP_REFERER']);
		}
		
	}
	
	
	/* 判断当前用的登录的类别 */
	public function userType(){
		//获取幼儿园id
		$uid=$_SESSION['uid'];
		if(empty($uid)){
			$this->error('您尚未登录，请登录',U('Login/login'));
		}
		//查询member表的pid  pid
		$member = M('member');
		$type_info = $member->field('uid,name,pid,level')->where("uid = $uid")->find();
		if(!empty($type_info)){
			if($type_info['level'] ==2){
				//教师登录
				$sc_id = $type_info['pid'];

			}elseif($type_info['level'] ==1){
					
				//幼儿园管理员登录
				$sc_id = $type_info['uid'];
	
			}elseif ($type_info['level'] ==0){
				//平台管理员登录
				$sc_id = $type_info['uid'];
			}else{
				//未获得等级
				$this->error('未获得等级信息',U('Index/index'));
			}
			return  $sc_id;
		}else{
			$this->error('未查询到登录信息，请重新登录',U('Login/logout'));
		}
			
	
	}
}